<script setup>
import { getCategoryFilter, getSubCategory } from '@/apis/getCategory';
import GoodsItem from './GoodsItem.vue';
import { onMounted, ref, watch } from 'vue';
import { useRoute } from 'vue-router';
const route = useRoute()
const categoryGoods = ref({})
const categoryData = async (id) => {
  const res = await getCategoryFilter(id);
  categoryGoods.value = res.result;
}
categoryData(route.params.id)
// 监听地址栏id变化
watch(route, (newValue) => { categoryData(newValue.params.id) })

// 获取数据
const categoryList = ref([])
const requsetData = ref({
  categoryId: route.params.id,
  page: 1,
  pageSize: 20,
  sortField: 'publishTime'
})
const categoryGoodsList = async () => {
  const res = await getSubCategory(requsetData.value);
  if (requsetData.value.page === 1) {
    categoryList.value = res.result.items.filter((item, index) => { if (index > 0) return item });
    if (requsetData.value.sortField === 'orderNum') return categoryList.value.sort((a, b) => b.orderNum - a.orderNum)
    return;
  }
  categoryList.value = res.result.items;
}
onMounted(() => categoryGoodsList())

const tabChange = () => {
  requsetData.value.page = 1
  categoryGoodsList()

}
const disabled = ref(false)
const load = async () => {
  requsetData.value.page++;
  const res = await getSubCategory(requsetData.value);
  categoryList.value = [...categoryList.value, ...res.result.items];
  if (requsetData.value.page === res.result.pageSize || res.result.items.length === 0) {
    disabled.value = true;
  }
}
</script>

<template>
  <div class="container ">
    <!-- 面包屑 -->
    <div class="bread-container">
      <el-breadcrumb separator=">">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/category/' + categoryGoods.parentId }">{{ categoryGoods.parentName
        }}</el-breadcrumb-item>
        <el-breadcrumb-item>{{ categoryGoods.name }}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="sub-container">
      <el-tabs v-model="requsetData.sortField" @tab-change="tabChange">
        <el-tab-pane label="最新商品" name="publishTime"></el-tab-pane>
        <el-tab-pane label="最高人气" name="orderNum"></el-tab-pane>
        <el-tab-pane label="评论最多" name="evaluateNum"></el-tab-pane>
      </el-tabs>
      <div class="body" v-infinite-scroll="load" :infinite-scroll-disabled='disabled'>
        <!-- 商品列表-->
        <GoodsItem v-for="goods in categoryList" :key="goods.id" :goods="goods" />
      </div>
    </div>
  </div>
  <div class="footer" v-if="disabled">
    <p>- - - - - - <span>没有更多了哦</span> - - - - - -</p>
  </div>
</template>



<style lang="scss" scoped>
.bread-container {
  padding: 25px 0;
  color: #666;
}

.sub-container {
  padding: 20px 10px;
  background-color: #fff;

  .body {
    display: flex;
    flex-wrap: wrap;
    padding: 0 10px;
  }

  .goods-item {
    display: block;
    width: 220px;
    margin-right: 20px;
    padding: 20px 30px;
    text-align: center;

    img {
      width: 160px;
      height: 160px;
    }

    p {
      padding-top: 10px;
    }

    .name {
      font-size: 16px;
    }

    .desc {
      color: #999;
      height: 29px;
    }

    .price {
      color: $priceColor;
      font-size: 20px;
    }
  }

  .pagination-container {
    margin-top: 20px;
    display: flex;
    justify-content: center;
  }
}

.footer {
  color: #fdcb6e;
  height: 30px;
  line-height: 30px;
  font-size: 16px;
  font-weight: 500;
  text-align: center;
  padding-top: 10px;

  span {
    color: #f39c12;
  }
}
</style>